<?php
namespace Home\Controller;
use Think\Controller;
class ContractController extends BackController {
	public function index(){
		$contract = M('contract');
		$where="";
		if(session('qe_user')['xid']!=0){
			$where['xid'] = array('eq',session('qe_user')['xid'],'and');
		}
		//名称搜索
		$contractName = I('post.contractName','','addslashes');
		if(!empty($contractName)){
			$where['contractName'] = array('like','%'.$contractName.'%','and');
			$this->assign('contractName',$contractName);
		}
		//编号
		$contractId = I('post.contractId','','addslashes');
		if(!empty($contractId)){
			$where['contractId'] = array('like','%'.$contractId.'%','and');
			$this->assign('contractId',$contractId);
		}
		$count      = $contract->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$this->all_count(1));// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$contract_data = $contract->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($contract_data as $k=>$v){
			$contract_data[$k]['xs_name'] = $this->username($v['salesman']);
		}
		$this->assign('contract_data',$contract_data);
		$this->assign('page',$show);
		$this->assign('count',$count);
		//var_dump($contract_data);
		$this->display();
	}
	public function newContract(){
		if(IS_POST){
			$contract = M('contract');
			$_POST['add_time'] = date('Y-m-d');
			$_POST['add_user'] = session('qe_user')['id'];
			$_POST['xid'] = session('qe_user')['xid'];
			$id = I('post.id','','int');
			if(!empty($id)){
				if($contract->create()) {
						if($contract->where('id='.$id)->save($_POST) !== false) {
								$this->success('修改成功',U('Contract/index'));
						}else {
								$this->error('修改失败',U('Contract/index'));
						}
				}else {
						$this->error($contract->getError());
				}
			}else{
				if($contract->create()) {
						if($contract->add($_POST)) {
								$this->success('新增成功',U('Contract/index'));
						}else {
								$this->error('新增失败',U('Contract/index'));
						}
				}else {
						$this->error($contract->getError());
				}
			}
		}else{
			$sc_student = M('sc_student');
			$contract = M('contract');
			$id = I('get.id','','int');
			$where="";
			if(session('qe_user')['xid']!=0){
				$where['xid'] = array('eq',session('qe_user')['xid'],'and');
			}		
			$where['state_id'] = array('eq',13,'and');
			$where['up'] = array('eq',0,'and');
			$student_data = $sc_student->where($where)->order('edit_time desc,id desc')->select();
			$this->assign('student_data',$student_data);
			//var_dump($sc_data);
			if(!empty($id)){
				$contract_data = $contract->where('id='.$id)->find();
				$sc_data = $sc_student->where('id='.$contract_data['student'])->find();
				$contract_data['curriculum_name'] = $this->curname($sc_data['curriculum_id']);
				$contract_data['discount_name'] = $this->disname($sc_data['discount']);
				$contract_data['xs_name'] = $this->username($contract_data['salesman']);
				$this->assign('contract_data',$contract_data);
			}
			//var_dump($contract_data);
			$this->display();
		}
	}
	public function student_show(){
		if(IS_POST){
			$student_id = I('post.student_id','','int');
			$sc_student = M('sc_student');
			$sc_data = $sc_student->where('id='.$student_id)->find();
			$sc_data['curriculum_name'] = $this->curname($sc_data['curriculum_id']);
			$sc_data['discount_name'] = $this->disname($sc_data['discount']);
			$sc_data['xs_name'] = $this->username($sc_data['xsid']);
			$this->ajaxReturn($sc_data);
		}
	}

	public function delContract(){
		$id = I('get.id');
		$res = M('contract')->where('id='.$id)->delete();
		if($res){
            $this->success('删除成功',U('Contract/index'));exit;
        }else{
            $this->error('删除失败',U('Contract/index'));exit;
        }
	}
}